import Vue from 'vue'
import store from './store'
import App from './App'
import './common/utils/filter.js'
import Json from './Json' //测试用数据
import uView from 'uview-ui';
import {
	serv
} from "./common/utils/config.js"
import request from "./common/utils/request.js"
import './style/iconfont.css'
import {
	getTheme,
	getThemeColor
} from './common/utils/theme.js'
/**
 *  因工具函数属于公司资产, 所以直接在Vue实例挂载几个常用的函数
 *  所有测试用数据均存放于根目录json.js
 *
 *  css部分使用了App.vue下的全局样式和iconfont图标，有需要图标库的可以留言。
 *  示例使用了uni.scss下的变量, 除变量外已尽量移除特有语法,可直接替换为其他预处理器使用
 */
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
const json = type => {
	//模拟异步请求数据
	return new Promise(resolve => {
		setTimeout(() => {
			resolve(Json[type]);
		}, 500)
	})
}
async function getSettings() {
	let indexType = "2";
	if (!store.state.header.Authorization && uni.getStorageSync("header")) {
		store.commit("setHeader", uni.getStorageSync("header"));
	}
	uni.setStorageSync('token', '');
	indexType = 1
	store.commit("setIndexType", indexType)
	const app = new Vue({
		...App
	})
	app.$mount()
	const theme = getTheme()
	if (theme !== 'blue') {
		uni.setTabBarStyle({
			selectedColor: getThemeColor(theme)
		})
		uni.setTabBarItem({
			index: 0,
			selectedIconPath: `static/${theme}/tab-home-current.png`
		})
		uni.setTabBarItem({
			index: 1,
			selectedIconPath: `static/${theme}/tab-cate-current.png`
		})
		uni.setTabBarItem({
			index: 2,
			selectedIconPath: `static/${theme}/tab-cart-current.png`
		})
		uni.setTabBarItem({
			index: 3,
			selectedIconPath: `static/${theme}/tab-my-current.png`
		})
	}
	return
	debugger
	const res = await request(serv.welfare + "/api/mobile/settings", {}).then(res => {
		console.log(res);
		if (res) {
			if (res.code == 0) {
				indexType = res.data.indexType;
				// if (!store.state.header.Authorization) {
					store.commit("setHeader", {
						"Authorization": "Bearer " + res.data.authToken
					});
				// }
				// deptInfo用array封装 同步Web端
				const deptinfo = [{
					deptId: res.data.settings.deptId,
					deptCode: res.data.settings.deptCode,
					tenantId: res.data.settings.tenantId
				}]
				uni.setStorageSync('portalSettingsInfo', res.data.settings);
				uni.setStorageSync('accountLoginValidType', res.data.settings.accountLoginValidType);
				if(res.data.settings&&res.data.settings.webTitle){
					document.title = res.data.settings.webTitle
				}
				uni.setStorageSync('token', res.data.authToken);
				uni.setStorageSync('deptinfo', deptinfo);
				uni.setStorageSync('enterpriseInfo', res.data.enterpriseInfo);
				uni.setStorageSync('theme', res.data.settings.themeColor)
				if (uni.getStorageSync('userInfo') && store.state.header.Authorization && store.state.header
					.currentDeptName) {
					store.commit('login')
				}
				store.replaceState(Object.assign({}, store.state, JSON.parse(sessionStorage.getItem(
					"store"))))
			} else {
				indexType = "1";
			}
		} else {
			indexType = '1';
		}
		store.commit("setIndexType", indexType)
		const app = new Vue({
			...App
		})
		app.$mount()
		const theme = getTheme()
		if (theme !== 'blue') {
			uni.setTabBarStyle({
				selectedColor: getThemeColor(theme)
			})
			uni.setTabBarItem({
				index: 0,
				selectedIconPath: `static/${theme}/tab-home-current.png`
			})
			uni.setTabBarItem({
				index: 1,
				selectedIconPath: `static/${theme}/tab-cate-current.png`
			})
			uni.setTabBarItem({
				index: 2,
				selectedIconPath: `static/${theme}/tab-cart-current.png`
			})
			uni.setTabBarItem({
				index: 3,
				selectedIconPath: `static/${theme}/tab-my-current.png`
			})
		}
	})
}

const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

Vue.prototype.$util = {
	msg
};
Vue.config.productionTip = false
Vue.prototype.$fire = new Vue();
Vue.prototype.$store = store;
Vue.prototype.$api = {
	msg,
	json,
	prePage
};
App.mpType = 'app'
Vue.use(uView);
Vue.mixin({
	data() {
		return {
			theme: '',
			themeColor: '#1890ff'
		}
	},
	onReady() {
		this.theme = getTheme()
		this.themeColor = getThemeColor(this.theme)
		let title = uni.getStorageSync("portalSettingsInfo").webTitle;
		if(title)
			document.title = title;
	},
	created() {
		if (sessionStorage.getItem("store")) {
			this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem(
				"store"))))
			uni.setStorageSync('header', this.$store.state.header)
		}

		window.addEventListener("beforeunload", () => {
			sessionStorage.setItem("store", JSON.stringify(this.$store.state))
		})
	}
})
getSettings();
